package com.avic.rong.userOrder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.avic.rong.network.Constant;
import com.avic.rong.userOrder.activity.MineOrderActivity;
import com.avic.rong.userOrder.activity.OrderConfirmActivity;
import com.avic.rong.userOrder.api.AddCarApi;
import com.avic.rong.userOrder.api.BuyAgainApi;
import com.avic.rong.userOrder.api.GetOrderTracesApi;
import com.avic.rong.userOrder.api.ToPayPXApi;
import com.avic.rong.userOrder.bean.BuyAgainBean;
import com.avic.rong.userOrder.bean.PayWayBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DeviceUtils;
import com.avic.rong.utils.DialogCustom;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by XinEr on 2019/8/12.
 * Describe:
 */
public class OrderAffair {
    public static OrderAffair orderAffair;
    public static Context context;
    //再次购买
    private  static int AGAINBUY=100;

    private OrderAffair(Context context) {
        this.context=context;
    }
    public static OrderAffair getInstance(Context context){
        if (orderAffair==null){
            synchronized (OrderAffair.class){
                if (orderAffair==null){
                    orderAffair=new OrderAffair(context);
                }
            }
        }
        return orderAffair;
    }

  /*  public static void lookWuLiu(Context context, String orderId) {
        if (!Constant.getToken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            if (TextUtils.isEmpty(orderId)) {
                ToastTools.showToast(context, "没有订单ID");
                return;
            }
            map.put("orderId", orderId);
             MineOrderActivity mineOrderActivity = (MineOrderActivity) context;
           GetOrderTracesApi getOrderTracesApi = new GetOrderTracesApi(mineOrderActivity,simpleOnNextListenerLookWuliu);
            getOrderTracesApi.setCache(false);
            getOrderTracesApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(getOrderTracesApi);

        }
    }
    public static void againBuyResq(Context context, String orderId) {
        if (!Constant.getToken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            if (TextUtils.isEmpty(orderId)) {
                ToastTools.showToast(context, "没有订单ID");
                return;
            }
            map.put("orderId", orderId);

            map.put("token", "w4/b+EQcZZnFlmpDOn6EJIZqU9OtdYXB/Zeu3JHGKygLF2xTVRyFnfxTic0l1spU1YMAkvlikvSlt0HxGkGr7gaN+noWbjhw");
            final MineOrderActivity mineOrderActivity = (MineOrderActivity) context;

            final BuyAgainApi buyAgainApi = new BuyAgainApi(mineOrderActivity, simpleOnNextListenerBuyAgain);
            buyAgainApi.setCache(false);
            buyAgainApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(buyAgainApi);

        }
    }

    private void toPayPX(int orderType, String orderSn,Context context) {

        if (!Constant.getToken().isEmpty()) {
            Map<String, String> paramsBody = new HashMap<>();

            if (TextUtils.isEmpty(orderSn)){
                ToastTools.showToast(context,"没有获取到订单编号");
                return;
            }
//            paramsBody.put("orderId", orderId+"");

            paramsBody.put("token",Constant.getToken());
            final MineOrderActivity mineOrderActivity = (MineOrderActivity) context;
            //获取支付方式
            final ToPayPXApi toPayPXApi = new ToPayPXApi(mineOrderActivity, simpleOnNextListenerPayWayPX);
            toPayPXApi.setCache(false);
            toPayPXApi.setMap(paramsBody);
            HttpManager.getInstance().doHttpDeal(toPayPXApi);


        }
    }

    private void toPayPX(int orderType, String orderSn,Context context) {

        if (!Constant.getToken().isEmpty()) {
            Map<String, String> paramsBody = new HashMap<>();
            if (TextUtils.isEmpty(orderSn)){
                ToastTools.showToast(context,"没有获取到订单编号");
                return;
            }
//            paramsBody.put("orderId", orderId+"");

            paramsBody.put("token",Constant.getToken());
            final MineOrderActivity mineOrderActivity = (MineOrderActivity) context;
            //获取支付方式
            final ToPayPXApi toPayPXApi = new ToPayPXApi(mineOrderActivity, simpleOnNextListenerPayWayPX);
            toPayPXApi.setCache(false);
            toPayPXApi.setMap(paramsBody);
            HttpManager.getInstance().doHttpDeal(toPayPXApi);


        }
    }

    private static void addCar(String skuStr, String orderType) {
        if (!Constant.getToken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            map.put("skuStr", skuStr);
            map.put("orderType", orderType);
//            是否再次购买（0/1）
            map.put("buyAgain", "1");
            map.put("token", Constant.getToken());
            map.put("deviceId", DeviceUtils.getIMEString(context));
            final MineOrderActivity mineOrderActivity = (MineOrderActivity) context;
            final AddCarApi addCarApi = new AddCarApi(mineOrderActivity, simpleOnNextListenerAddCar);
            addCarApi.setCache(false);
            addCarApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(addCarApi);
        }
    }

    //查看物流
   public static HttpOnNextListener simpleOnNextListenerLookWuliu = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }
        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    //再次购买
   public static HttpOnNextListener simpleOnNextListenerBuyAgain = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }
            final BuyAgainBean data = (BuyAgainBean) subjects.getData();
            if (subjects.getCode().equals("0000")) {
                if (data.getOrderType() == 4) {

                    boolean isSkuStr = data.isIsSkuStr();
                    if (subjects.getMsg().length() > 0) {
                        if (isSkuStr) {
                            DialogCustom.showDoubleDialog(context, "提示", subjects.getMsg() + "是否继续再次购买?", "取消", "确定", new DialogCustom.CustomDialogDouble() {
                                @Override
                                public void rightButtonClicked() {
                                    //添加购物车
                                    addCar(data.getSkuStr(), data.getOrderType() + "");

                                }

                                @Override
                                public void leftButtonClicked() {

                                }
                            });
                        } else {
                            ToastTools.showToast(context, subjects.getMsg());
                        }
                    } else {
                        //添加购物车
                        addCar(data.getSkuStr(), data.getOrderType() + "");
                    }
                } else {
                    if (subjects.getMsg().length() > 0) {
                        ToastTools.showToast(context, subjects.getMsg());
                    } else {
                        //跳转确认订单页
                        Intent intent = new Intent(context, OrderConfirmActivity.class);
                        intent.putExtra("orderType", data.getOrderType() + "");
                        intent.putExtra("skuStr", data.getSkuStr());
                        intent.putExtra("againBuy",AGAINBUY);
                        context.startActivity(intent);
                    }
                }
            } else {
                ToastTools.showToast(context, subjects.getMsg());
            }
        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    //培训获取支付方式
    HttpOnNextListener simpleOnNextListenerPayWayPX = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            paymentList.clear();
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                return;
            }

            PayWayBean data = (PayWayBean) subjects.getData();
            paymentList.addAll(data.getPaymentList());
            //选择支付
            selectPay(data);


        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };*/

}
